﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using dbutility;
using DBUtility;
using System.Collections.Generic;

namespace HFModel.DAL
{
    public class SubwebsiteDAL
    {
        public List<HFModel.VO.SubwebsiteModel> GetSourceList(int PageSize, int PageNumber, string strWhere, out int total)
        {

            DataSet ds = PageHelper.GetList(PageSize, PageNumber, "tb_Subwebsite", "ID", strWhere, out total);

            //实例化列表
            List<HFModel.VO.SubwebsiteModel> list = new List<HFModel.VO.SubwebsiteModel>();

            //判断ds是不是空的
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //把datatable转换成对象
                    HFModel.VO.SubwebsiteModel m = DBHelperSQL.SetModel<HFModel.VO.SubwebsiteModel>(dr);

                    if (m != null)
                        list.Add(m);
                }
            }

            return list;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(HFModel.VO.SubwebsiteModel model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@CityID", SqlDbType.Int,4),
					new SqlParameter("@CityName", SqlDbType.VarChar,50) ,
                                        new SqlParameter("@Url" ,SqlDbType.VarChar ,255)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.CityID;
            parameters[2].Value = model.CityName;
            parameters[3].Value = model.Url;
            DBHelperSQL.RunProcedure("tb_Subwebsite_Add", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update(HFModel.VO.SubwebsiteModel model)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@CityID", SqlDbType.Int,4),
					new SqlParameter("@CityName", SqlDbType.VarChar,255),
                                        new SqlParameter("@Url" ,SqlDbType.VarChar ,255)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.CityID;
            parameters[2].Value = model.CityName;
            parameters[3].Value = model.Url;
            DBHelperSQL.RunProcedure("tb_Subwebsite_Update", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            int rowsAffected = 0;
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            DBHelperSQL.RunProcedure("tb_Subwebsite_Delete", parameters, out rowsAffected);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
